
Home page
INTRODUCTION TO J2ME!
J2ME stands for Java 2 Microedition. It is a scaled-down version of the traditional Java language. Since mobile phones have limited processing power and memory, J2ME was developed to cover these low memory devices with very low resources. Traditional Java language is too heavy for a mobile device to run. In J2ME, every application is a MIDlet. A MIDlet can exist on it's own. In this tutorial, we will be using an application called J2ME SDK Mobile. It is a Software Development Kit for Mobile phones and runs well on java s40 phones too. Download it at the bottum of this tutorial. After downloading the zip file, rename it to J2ME_SDK_Mobile_jar using blue ftp. After that, close blue ftp and rename J2ME_SDK_Mobile_jar to J2ME_SDK_Mobile.jar using your phone and the application will be installed. Launch the application. When the application is fully lauched, navigate the mouse pointer to "New project" and click on it. In the Project Name, delete everything and write HelloGuys. Scroll to the Project Loction and edit it like this: E:/myfirstproject/ . Now click Create. If the application asks for permision to read and write data, press Yes until the application takes you to the next screen. Select Resource Package. You will be shown an empty screen. Press Options and select Project Properties. Now select Build. Select Compressing JAR. Now Edit HelloGuys.jad to HelloGuys_jad and edit HelloGuys.jar to HelloGuys_jar . Press Ok and go back to the previous screen. Press Options and select New. You will be provided with some list, click on MIDlet and press Next. Now edit the MIDlet Name as HelloGuys. Also edit the MIDlet class as HelloGuys and in the MIDlet icon, write kill.png . After that, press finish. You will be taken to the source code editor screen. Now press Menu and navigate to the edit button and click on it. Make sure you edit the source code so that it resembles the one written in green just below. After you've edited everything, press Menu and select Build. If you're asked any permission, press yes. After sometime, the application will exit automatically. Restart the application again. This time, it will start compiling. If there are no errors, it will ask for permission to start when notified. Press Yes and it will exit automatically again. Then restart it again. This time it will start preverification. If this step is successful, you will be asked whether to allow application start automatically when notified. Again, press Yes. The application will be exited again. Restart it again. NB: IF THE APPLICATION AUTOMATICALLY EXITS, IT LEAVES BEHIND A REMINDER AT THE PHONE'S STANDBY MODE. SO YOU CAN ALSO GO BACK TO STANBY MODE AND START THE APPLICATION FROM THERE. BUT SOMETIMES IT MIGHT NOT LEAVE A REMINDER. This time it will attemp creating HelloGuys.jar file, but it won't be successful because no third party application is allowed to create a .jar file directly in Java s40 phones. Because of this you will get an error message. Don't worry, just press Back to return to the source editor screen and now exit the application. Now open your blue ftp or any file manager. Open the memory card root(which is drive E). Locate myfirstproject and open it. Open HelloGuys folder, open Build folder. You will see Compiled folder, Preverified folder and a manifest.mf file. Copy the manifest.mf and open the Preverified folder. You will see HelloGuys.class. Press Menu and select Create folder. Rename the folder as META-INF and open it. Paste the file you copied into the META-INF folder by pressing 3. Using blue ftp, look for a png image and rename it to kill.png. Copy it and paste inside the Preverified folder. Open the Preverified folder and highligh the META-INF folder, the HelloGuys.class and the kill.png by pressing the # button three times. Now press Menu and select Compress Jar. Select OK and compression will begin. After compression is complete, exit blue ftp. Now rename your compressed file from _jar to .jar using your phone and launch the application!
LIFE-CYCLE OF A MIDlet: A MIDldt undergoes three stages in it's life-cycle. These are paused state, active state and the destroyed state. In the paused state, the MIDlet is virtually doing nothing. After launching, the MIDlet enters the active state. All processes take place in the active state. The last state is the destroyed state. The MIDlet enters this state when it wants to exit. The various states have their corresponding methods which are called to usher the MIDlet to that state. The startApp() method is called to usher the MIDlet in the active state. The pauseApp() method is called in order to enter the paused state. destroyedApp() method is also called to enter the destroyed state.
A SIMPLE PROGRAM TO DISPLAY "Hello Guys!" ON THE SCREEN.
import javax.microedition.lcdui.*;import javax.microedition.midlet.*;public class HelloGuys extends MIDlet {
Form form;
Display display;
public HelloGuys() {
form = new Form("Hello Guys!");
}
public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
J2ME SDK Mobile
ANALYSIS OF THE ABOVE PROGRAM
=>Now, let's try to analyse the program above. The import keyword tells the compiler to find packages which are required for execution of the program. Packages are collection of classes(libraries) containing portable Java bytecode files. We have imported two main packages:
javax.microedition.midlet.*;
javax.microedition.lcdui.*;
The first package contains bytecode files of the MIDlet class. The second package contains various classes such as Form, Canvas, TextBox, TextField, List, etc(these will be discussed latter). Every program begins with the importation of packages using the import keyword. lcdui stands for Liquid Crystal Display User Interface.
=>The next thing is declaring HelloGuys class as "public" and extending the MIDlet class. Every J2ME program is a class and the program starts with the name of the class(in this case, the name of our class is HelloGuys). The HelloGuys class must extend the MIDlet class by using the "extend" keyword and is declared public by using the "public" keyword or access modifier. We extend HelloGuys to MIDlet because, HelloGuys is a subclass of MIDlet class. Every class begins with an opening curly bracket "{" and ends with a closing curly bracket "{". Everything is done this way:
public class HelloWorld extends MIDlet {
=>The next thing we did is declaring our identifiers(in this case, our identifiers are "form" and "display") as Form and Display objects. You can use any name as your identifiers but there are rules governing the characters an identifier's name should contain. Next, we have the HelloGuys() constructor also declared as "public". Here is how we declare and assign at once:
Form form = new Form("HelloGuys");
=>A startApp() method is required for execution of the program to take place. It is the first method that gets executed. What i'm trying to say is that, the program starts execution at immediately startApp() is called by the AMS. This method begins with an opening curly bracket and ends with a closing curly bracket. Anything written between these two curly brackets tells your device what to do when the program is first run. In our case, we have the following instructions between these two curly brackets:
display = Display.getDisplay(this);
display.setCurrent(form);
The second command "display.setCurrent(form);", tells the program or sets the object that is to be displayed. In this case, the object we want to display is "form". The first command, which is "display = Display.getDisplay(this);" tells the program to call the display object. Remember that the form object has "Hello Guys!" as its argument. So don't expect the program to display the word form, but it will display what is contained in the form object(which is Hello Guys!. You remember we wrote "form = new Form("Hello Guys!");". In Java, what ever is at the right hand side of the equall sign is kept or stored in what ever is in the left hand side. In this case, "form" is on the left side of the equal sign and new Form("Hello Guys!"); is on the right handside. The "new" keyword tells the program to create a Form object. A Form is a displayable class for displaying items. The second thing on the right handside is ("Hello Guys!"); which is an argument passed into the "form" object. NOTICE: "Form" and "form" are not the same.
"Form" is a class or which allows items to be displayed on your phone, but "form" is a created object which can contain a literal print or other items. A literal print refers to anything enclosed in a pair of quotation marks. A J2ME program displays everything that is between a pair of quotation marks using the display.getDisplay(this); method. In Java, every line of instructions ends with a semi colon(;). The semi colon tells the program that the current line has come to an end and that, it jumps to the next line to begin reading.=>The pauseApp() method is called when ever a MIDlet is to enter into the inactive state. You can see that there is nothing between the two curly brackets after the pauseApp() method. This is because, the MIDlet does nothing when it is in the pause state. It only notifies your device that it is in an inactive state.
=> destroyApp() is called when the MIDlet wants to exit.
Bye for now. In my next post i will explain the other user interface elements like List, TextField, etc in order to prepare you towards writing other J2ME programs.
All rights reserved
© sirwisdom.tk 2012-2013